package com.handinfo.android.uicontrols;

import java.util.Vector;

import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;

public class DWControlTools
{
	
	
	/**
	 * 绘制一个标签
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param isSel 是否选中
	 */
	public static final void drawLabel(DWGraphics g, int x, int y, int w, int h, boolean isSel)
	{
		if (isSel) {
			drawLabel(g, x, y, w, h, 0xFF180100, 0xFFe0e16a, 0xFF461a17, 0xFFb6554d, 0xFF92422e, 0xFF753c2e, 0xFF768768);
		}
		else {
			drawLabel(g, x, y, w, h, 0xFF230200, 0xFFd5c181, 0xFF685a4b, 0xFFdb867b, 0xFFb37868, 0xFF91685e, 0xFF473218);
		}
	}

	/**
	 * 
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param color1
	 * @param color2
	 * @param color3
	 * @param color4
	 * @param color5
	 * @param color6
	 * @param color7
	 */
	private static final void drawLabel(DWGraphics g, int x, int y, int w, int h, int color1, int color2, int color3,
			int color4, int color5, int color6, int color7)
	{
		drawRect(g, x, y, w, h, color1, 0);
		drawRect(g, x, y, w, h, color2, 1);
		drawRect(g, x, y, w, h, color3, 2);

		int tempX = x + 3;
		int tempY = y + 3;
		int tempW = w - 6;// fill 小1
		int tempH = ((h - 7) >> 1);
		g.setColor(color4);
		g.drawLine(tempX, tempY, tempX + tempW, tempY);

		tempY += 1;
		g.setColor(color5);
		g.fillRect(tempX, tempY, tempW + 1, tempH);
		g.setColor(color6);
		tempY += tempH;
		tempH = h - 7 - tempH;
		g.fillRect(tempX, tempY, tempW + 1, tempH);
		g.setColor(color5);
		tempY = y + h - 3;
		g.drawLine(tempX, tempY, tempX + tempW, tempY);
	}

	/**
	 * 绘制单个列表框里的边框
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param color
	 * @param interval
	 */
	public static void drawRect(DWGraphics g, int x, int y, int w, int h, int color, int interval)
	{
		g.setColor(color);
		g.drawRect(x + interval, y + interval, w - (interval << 1), h - (interval << 1));
	}

	/**
	 * 绘制单个列表框里的填充
	 * 
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param color
	 * @param interval
	 */
	public static void fillRect(DWGraphics g, int x, int y, int w, int h, int color, int interval)
	{
		g.setColor(color);
		g.fillRect(x + interval, y + interval, w - (interval << 1) + 1, h - (interval << 1) + 1);
	}

	/**
	 * 绘制窗体带标题
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public static final void drawWindowBackgroud(DWGraphics g, int x, int y, int w, int h)
	{
		g.setClip(x, y, w, h);
		g.setColor(0xFF3B2415);
		g.fillRect(x, y, w, h);
		for (int i = 0, length = COLOR_WIN_BORDER.length; i < length; i++) {
			drawColorRect(g, x + i, y + i, w - (i << 1) - 1, h - (i << 1) - 1, COLOR_WIN_BORDER[i]);
		}
	}

	private static final int[] COLOR_WIN_BORDER = { 0xFF580D00, 0xFFFFFFE1, 0xFF484232, 0xFF230200 };

	/**
	 * d 绘制一个带颜色的框
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param color
	 */
	public static final void drawColorRect(DWGraphics g, int x, int y, int w, int h, int color)
	{
		g.setColor(color);
		g.drawRect(x, y, w, h);
	}

	/**
	 * 绘制带边框的盒子
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public static final void paintBox(DWGraphics g, int x, int y, int w, int h)
	{
		//左顶点
		g.drawRect(x + 2, y + 2, 2, 4);
		g.drawRect(x + 2, y + 2, 4, 2);
		//右顶点
		g.drawRect(x + w - 6, y + 2, 4, 2);
		g.drawRect(x + w - 4, y + 2, 2, 4);
		//左下顶点
		g.drawRect(x + 2, y + h - 6, 2, 4);
		g.drawRect(x + 2, y + h - 4, 4, 2);
		//右下顶点
		g.drawRect(x + w - 6, y + h - 4, 4, 2);
		g.drawRect(x + w - 4, y + h - 6, 2, 4);
	}

	/**
	 * 绘制选中框
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public static final void paintSelItem(DWGraphics g, int x, int y, int w, int h)
	{
		drawRect(g, x, y, w, h, 0xFF946300, 0);
		drawRect(g, x, y, w, h, 0xFFFFD447, 1);
		fillRect(g, x, y, w, h, 0xFF8C5A5A, 2);
	}

	/**
	 * 获得字符串最大宽度
	 * @param menus
	 * @return
	 */
	public static final int getMaxWidth(String[] menus)
	{
		if (menus == null || menus.length == 0) {
			return 0;
		}
		int maxWidth = 0;
		for (int i = 0, length = menus.length; i < length; i++) {
			if (menus[i] == null) {
				continue;
			}
			int tempW = (int) DWFont.getDefaultFont().stringWidth(menus[i]);
			if (tempW > maxWidth) {
				maxWidth = tempW;
			}
		}
		return maxWidth;
	}

	/**
	 * 获得取值范围在最小值与最大值之间的数【min,max】
	 * @param num
	 * @param min
	 * @param max
	 * @return
	 */
	public static final int getMidNum(int num, int min, int max)
	{
		if (num <= min) {
			return min;
		}
		else if (num >= max) {
			return max;
		}
		else {
			return num;
		}
	}

	/**
	 * 将字符串转换为字符串数组
	 * 
	 * @param content
	 * @param font
	 * @param maxWidth
	 * @return
	 */
	public static String[] getStrArray(String content, DWFont font, int maxWidth)
	{
		return getStrArray(content, font, CHANGE_ROW, maxWidth);
	}

	public static final char CHANGE_ROW = '`';

	/**
	 * 将指定的字符串指定的字符分割为字符串数组
	 * 
	 * @param inf
	 * @param tag 标记字符
	 * @param width 宽度限制，到达宽度则换行
	 * @param line 是否支持`换行 true不支持, false支持
	 * @return
	 */
	public static String[] splitString(String inf, DWFont font, char tag, int width, int x, boolean line)
	{
//		if (inf == null) {
//			return null;
//		}
//		int start = 0, end = 0, length = x;
//		String str = inf;
//		int tmp = 1;
//		// 计算出分割后数组大小
//		while (end < str.length()) {
//			if (str.charAt(end) == tag
//					|| str.charAt(end) == '\n'
//					|| (width != 0 && length + font.charWidth(str.charAt(end)) >= width)) {
//				tmp++;
//				length = 0;
//			}
//			length += font.charWidth(str.charAt(end));
//			end++;
//		}
//		String[] s_control = new String[tmp];
//		s_control[0] = "";
//		tmp = 0;
//		end = 0;
//		length = 0;
//		// 遍历所有字符来进行分割
//		while (end < str.length()) {
//			while (end < str.length()
//					&& str.charAt(end) != tag
//					&& str.charAt(end) != '\n'
//					&& (width == 0 || (width != 0 && length + font.charWidth(str.charAt(end)) < width))) {
//				length += font.charWidth(str.charAt(end));
//				end++;
//			}
//			s_control[tmp] = str.substring(start, end);
//			tmp++;
//			length = 0;
//			if (end < str.length() && (str.charAt(end) == tag || str.charAt(end) == '\n')) {
//				end++;
//			}
//			start = end;
//		}
//		while (s_control[s_control.length - 1] == null) {
//			String[] s_tmp = new String[s_control.length - 1];
//			arraycopy(s_control, 0, s_tmp, 0, s_tmp.length);
//			s_control = null;
//			s_control = s_tmp;
//		}
//		return s_control;
		return cutString(font, inf, width, tag, line);
	}

	public static String[] cutString(DWFont sysFont, String sbsrc, int length, char tag, boolean line)
	{
		String helpContext[] = null;
		Vector<String> strVector = new Vector<String>();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < sbsrc.length(); i++) {
			if (sysFont.stringWidth(sb.toString() + sbsrc.charAt(i)) > length
					|| (sbsrc.charAt(i) == tag && !line)) {
				strVector.add(sb.toString());
				sb.delete(0, sb.toString().length());
				if (sbsrc.charAt(i) != tag)
					sb.append(sbsrc.charAt(i));
			}
			else
				sb.append(sbsrc.charAt(i));
		}
		strVector.add(sb.toString());
		helpContext = new String[strVector.size()];
		strVector.copyInto(helpContext);
		return helpContext;
	}

	/**
	 * 数组复制
	 * 
	 * @param object
	 * @param _int
	 * @param object2
	 * @param _int3
	 * @param _int4
	 */
	public static void arraycopy(Object object, int _int, Object object2,
			int _int3, int _int4)
	{
		System.arraycopy(object, _int, object2, _int3, _int4);
	}

	public static String[] getStrArray(String content, DWFont font, char tag, int maxWidth)
	{
		if (content == null) {
			return null;
		}
		String[] temp = new String[30];
		int rowLength = 0;// 行长度
		char tempChar = 0;
		int start = 0;
		int row = 0;// 行指针
		int charlength = 0;
		char[] chars = content.toCharArray();
		char changeRow = '\n';
		for (int i = 0, size = content.length(); i < size; i++) {
			tempChar = chars[i];
			charlength = (int) font.charWidth(tempChar);
			int sumW = rowLength + charlength;
			if (tempChar == tag || tempChar == changeRow) {// 不能将其放入到字符串中
				if (i <= start) {// 当前就是第一个
					temp[row] = "";
				}
				else {
					temp[row] = content.substring(start, i);
				}
				row++;
				start = i + 1;
				rowLength = 0;
			}
			else if (sumW > maxWidth) {
				temp[row] = content.substring(start, i);
				start = i;
				rowLength = charlength;
				row++;
			}
			else if (sumW == maxWidth) {
				temp[row] = content.substring(start, i);
				row++;
				start = i;
				rowLength = 0;
			}
			else {
				rowLength += charlength;
			}

			if (row >= temp.length) {
				String[] temp2 = new String[temp.length + 10];
				System.arraycopy(temp2, 0, temp, 0, temp.length);
				temp = null;
				temp = temp2;
				temp2 = null;
			}
		}
		if (start < chars.length) {
			temp[row] = content.substring(start, content.length());
		}
		String[] result = null;
		result = new String[row + 1];
		for (int i = 0, length = result.length; i < length; i++) {
			result[i] = temp[i];
		}
		return result;
	}
}
